package com.faxsun.controller.fraud;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.broadleafcommerce.common.web.controller.BroadleafAbstractController;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faxsun.core.web.security.service.FraudService;
import com.faxsun.fm.model.Response;

/**
 * 风控相关接口
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
@Controller
@RequestMapping("/fraud")
public class FraudController extends BroadleafAbstractController {

	@Resource(name = "fsFraudService")
	protected FraudService fraudService;

	@RequestMapping(value = "/loginFraud", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String loginFraudAction(HttpServletRequest request,
			HttpServletResponse response) {

		// 查询风控策略
		Response resp = fraudService.queryFraudForLogin(request);

		return responseToJson(resp);
	}

	@RequestMapping(value = "/registerFraud", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String registerFraudAction(HttpServletRequest request,
			HttpServletResponse response) {

		// 查询风控策略
		Response resp = fraudService.queryFrauadForRegister(request);

		return responseToJson(resp);
	}

	private String responseToJson(Response resp) {
		if (resp != null) {
			// 需要返回策略action
			ObjectMapper mapper = new ObjectMapper();
			try {
				return mapper.writeValueAsString(resp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";
	}
}
